/*
 * Copyright 2015 Ming Xia
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.healthcloud.controller;

import com.healthcloud.entity.SysUserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.healthcloud.constants.UserConsts;
import com.healthcloud.service.UserService;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author Sephy
 * @since: 2015-05-31
 */
@ApiIgnore
//@Controller
public class LoginController {

	@Autowired
	private UserService userService;

	/**
	 * 登录页面
	 *
	 * @return
	 */
	@RequestMapping(value = "/login",method=RequestMethod.GET)
	public Object loginGet() {
		return "login2";
	}

	/**
	 * 登录页面
	 *
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/login",method=RequestMethod.POST)
	public Object loginPost(Model model) {
		model.addAttribute("errorMsg","帐号密码错误");
		return "login2";
	}

	/**
	 * 用户退出处理
	 *
	 * @return
	 */
		@RequestMapping(value = "/logout")
		public Object logout() {
			Subject subject = SecurityUtils.getSubject();
			if (subject.isAuthenticated()) {
				subject.logout();
			}
			return "/login";
		}

	/**
	 * 注册页面
	 * @return
	 */
	@RequestMapping(value = "/signup", method = RequestMethod.GET)
	public Object signup() {
		return "signup";
	}

	/**
	 * 处理注册信息
	 *
	 * @param mobile
	 * @param username
	 * @param password
	 * @return
	 */
	@RequestMapping(value = "/signup", method = RequestMethod.POST)
	public Object signup(String mobile, String username, String password) {
		// 参数验证
		SysUserEntity newUser = userService.register(mobile, username, password, Integer.valueOf(UserConsts.KIND_NORMAL));
		return "redirect:/signup_success";
	}

	/**
	 * 注册成功跳转
	 * @return
	 */
	@RequestMapping("/signup_success")
	public Object signupSuccess() {
		return "signup_success";
	}
}
